<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

if(!$_SESSION) session_start();
// Connect to the database
////////////////////////////////////////////////////////

require_once("../../scripts/config.php");

// Load Functions
////////////////////////////////////////////////////////

require_once("../../scripts/functions.php");

if($_GET['download'] != 'off'){
	
	header("Content-Type: application/json");

	header("Content-Disposition: attachment; filename=".$_GET['values']."_json.txt;");

}

// Step 1 Check User Email Against DB
///////////////////////////////////////////////////////

if(!isset($_GET['apikey']) || (strlen($_GET['apikey']) == 0)) die('API Key is missing');

$sql_chk = sprintf("SELECT * FROM tblusers WHERE email_address = %s",GetSQLValueString($_GET['apikey'], "text"));

$user = sqlLoad($sql_chk, $db); 

if(sizeof($user)==0) die('Incorrect API Key');

// Step 2 setup the SQL
///////////////////////////////////////////////////////

if($_GET['values'] == "projects") $sql = "SELECT ProjectID, project_label FROM tblprojects WHERE set_active = 1";

if($_GET['values'] == "users") $sql = "SELECT UserID, name_display FROM tblusers WHERE set_active = 1";

if(strlen($sql) == 0) die('No information requested');

$result = sqlLoad($sql, $db); 

if(sizeof($result) == 0) die('No data to return');

// Step 3 Output JSON
///////////////////////////////////////////////////////

echo json_encode($result);